﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Globalization;

namespace Lapas.Common
{
    public partial class Helper
    {
        /// <summary>
        /// Thực hiện trả ra xâu dạng lower case, ký tự đầu viết hoa
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        /// 
        public static string ToUpperCaseFirstChar(string inputString)
        {
            if (string.IsNullOrEmpty(inputString)) return string.Empty;
            if (inputString.Length == 1) return inputString.ToUpper();
            return string.Format("{0}{1}", inputString.ToUpper()[0], inputString.Substring(1).ToLower());
        }
        public static bool IsValidCardIdentity(string idString)
        {
            Regex reg = new Regex(@"[^\w\.-]", RegexOptions.CultureInvariant);
            return (!reg.Match(idString).Success);
        }

        public static string CleanInput(string strIn)
        {
            // Replace invalid characters with empty strings.
            return Regex.Replace(strIn, @"[^\w\.-]", "");
        }

        public static string GetContractNumberPrefix()
        {
            string contractNumberPrefix = AppSettings.ContractNumberFormat;
            string[] paras = contractNumberPrefix.Split(new char[] { '[' });
            foreach (string item in paras)
            {
                if ((!string.IsNullOrEmpty(item)) && (item.IndexOf("]") > 0))
                {
                    string para = item.Substring(0, item.IndexOf("]"));
                    contractNumberPrefix = ReplaceParam(contractNumberPrefix, para);
                }

            }
            if (contractNumberPrefix.Length > 14)
                contractNumberPrefix = contractNumberPrefix.Substring(0, 14);
            return contractNumberPrefix;
        }
        public static string UrlSanitize(string url)
        {
            url = Regex.Replace(url, @"\s+", " ");
            string stFormD = url.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[ich]);
                }
            }

            return (sb.ToString().Replace("đ", "d").Replace("Đ", "D"));
        }

        private static string ReplaceParam(string contractNumberPrefix, string para)
        {
            if (string.IsNullOrEmpty(para)) return contractNumberPrefix;
            string result = contractNumberPrefix;
            switch (para.Substring(0,1))
            { 
                case "B":
                    result = result.Replace("[" + para + "]", AppSettings.BranchCode);
                    break;
                case "Y":
                    int i = 0;
                    string yearStr = AppSettings.CurrentTransactionDate.Year.ToString();
                    if ((para.Length > 1) && (int.TryParse(para.Substring(1,1),out i)))
                    {
                        if (i > yearStr.Length) i = yearStr.Length;
                        result = result.Replace("[" + para + "]", yearStr.Substring(yearStr.Length - i, i));
                    }
                    else
                        result = result.Replace("[" + para + "]", yearStr);
                    break;
                case "M":
                    string m = string.Empty;
                    if (AppSettings.CurrentTransactionDate.Month < 10)
                        m = "0" + AppSettings.CurrentTransactionDate.Month.ToString();
                    else
                        m = AppSettings.CurrentTransactionDate.Month.ToString();
                    result = result.Replace("[" + para + "]", m);
                    break;
                case "D":
                    string d = string.Empty;
                    if (AppSettings.CurrentTransactionDate.Day < 10)
                        d = "0" + AppSettings.CurrentTransactionDate.Day.ToString();
                    else
                        d = AppSettings.CurrentTransactionDate.Day.ToString();
                    result = result.Replace("[" + para + "]", d);
                    break;
            }
            return result;
        }


    }
}
